//Documentation complete
/**
 * @author Kazu
 * @version 2009MR22
 */

package problemDomain;

import java.util.List;
import java.util.Iterator;

import static problemDomain.ErrorBean.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.*;

/**
 * Represents a menu which contains menu items.
 */
public class MenuBean implements Bean {
	
	/* Constants */
	
	private static final long serialVersionUID = 1999214996708397871L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	private int id;
	private String name;
	private String description;
	private List<MenuItemBean> itemList;
	private Boolean active;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>MenuBean</code> with default values.
	 */
	public MenuBean () {
		id = 0;
		name = "";
		description = "";
		itemList = null;
		active = null;
	}
	
	/**
	 * Constructs a <code>MenuBean</code> with an ID number.
	 * @param id the ID number
	 */
	public MenuBean (int id) {
		this();
		this.id = id;
	}
	
	/**
	 * Constructs a <code>MenuBean</code> with all specified values.
	 * @param id The ID number of the menu
	 * @param name The name of the menu
	 * @param description The description of the menu
	 * @param itemList The list of items in the menu
	 * @param active The active status of the menu
	 */
	public MenuBean (int id, String name, String description,
		List<MenuItemBean> itemList, Boolean active) {
    	this.id = id;
    	this.name = name;
    	this.description = description;
    	this.itemList = itemList;
    	this.active = active;
    }

	/* END Constructors */
	
	
	/* Accessors */
	
	/**
	 * Returns the ID number of the menu.
	 * @return The ID number of the menu
	 */
	public int getId () {
		return id;
	}

	/**
	 * Returns the name of the menu.
	 * @return The name of the menu
	 */
	public String getName () {
		return name;
	}
	
	/**
	 * Returns the description of the menu.
	 * @return The description of the menu
	 */
	public String getDescription () {
		return description;
	}
	
	/**
	 * Returns the list of items in the menu.
	 * @return The list of items in the menu
	 */
	public List<MenuItemBean> getItemList () {
		return itemList;
	}

	/**
	 * Returns <code>true</code> if the menu is active.
	 * @return <code>true</code> if the menu is active 
	 */
	public Boolean getActive () {
		return active;
	}
	
	/* END Accessors */
	
	
	/* Mutators */	
	
	/**
	 * Sets the ID number of the menu.
	 * @param id the ID number of the menu to set
	 */
	public void setId (int id) {
		this.id = id;
	}
	
	/**
	 * Sets the name of the menu.
	 * @param name the name of the menu to set
	 */
	public void setName (String name) {
		this.name = name;
	}

	/**
	 * Sets the description of the menu.
	 * @param description the description of the menu to set
	 */
	public void setDescription (String description) {
		this.description = description;
	}

	/**
	 * Sets the list of items in the menu.
	 * @param itemList the list of items in the menu to set
	 */
	public void setItemList (List<MenuItemBean> itemList) {
		this.itemList = itemList;
	}

	/**
	 * Sets the active status of the menu 
	 * @param active the active status of menu to set
	 */
	public void setActive (Boolean active) {
		this.active = active;
	}
	
	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		if (id < 0)
			throwError(
				MENU_ID_INVALID,
				String.valueOf(id),
				this);
		
		if (name != null && name.length() > 25)
			throwError(
				MENU_NAME_TOO_LONG,
				name,
				this);
		
		if (description != null && description.length() > 255)
			throwError(
				MENU_DESCRIPTION_INVALID,
				description,
				this);
		
		if (active == null)
			throwError(
				MENU_ACTIVE_NULL,
				this);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<menu>");
		sb.append("<id>" + id + "</id>");
		sb.append("<name>" + name + "</name>");
		sb.append("<description>" + description + "</description>");
		sb.append(encapsulateBeanListXML(itemList, "menuitems"));
		sb.append("<active>" + active + "</active>");
		sb.append("</menu>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Menu ID: " + id + '\n');
		sb.append("Menu Name: " + name + '\n');
		sb.append("Description: " + description + '\n');
		sb.append("Item List:\n");
		if (itemList != null) {
			Iterator<MenuItemBean> i = itemList.iterator();
			while (i.hasNext())
				sb.append('\t' + i.next().toString().replaceAll("^(.*)$", "\t$1") + '\n');
		} else {
			sb.append("\tItem list is null.\n");
		}
		sb.append("Active: " + active + "\n\n");
		
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
}
